#include "drv_rgb.h"
#include "SC_Init.h"
#include "toolkit.h"
#include "stdio.h"
#include "tm1668.h"
#define RGB_RED_PWM   PWM04
#define RGB_GREEN_PWM PWM05
#define RGB_BLUE_PWM  PWM06

#define FAN_CONTROL_PWM  PWM07

//#define BUZZER_PWM PWM20
#define BUZZER_PIN P23
#define DISINFECT_PIN P12
#define ION_PIN P33

#define FAN_LEVEL_MAX 6
#define FAN_SLEEP_LEVEL 6
//unsigned int code fanLevelValue[FAN_LEVEL_MAX+1] = {0, 48, 68, 75, 82, 87, 33};//!!!Nidec new
unsigned int code fanLevelValue[FAN_LEVEL_MAX+1] = {0, 48, 75, 87, 87, 87, 33};//!!!Nidec new
//unsigned int code fanLevelValue[FAN_LEVEL_MAX+1] = {0, 35, 47, 51, 55, 62 ,29};//!!!Nidec old
//unsigned int code fanLevelValue[FAN_LEVEL_MAX+1] = {0, 34, 48, 52, 57, 62 ,27};//!!!max value 100
unsigned char xdata fan_real_level = 0;
bit disInfectWorkOnFlag = 0;
bit IonWorkOnFlag       = 0;
// bool filterWorkOnFlag =FALSE;


extern uint8_t device_timeout_count(uint32_t* time_enter,uint32_t time_out);

//extern uint8_t xdata fanDebugValue;
//extern uint8_t xdata fanDebugFlag;

extern bit BeepOnFlag;

rgb_color_e xdata colorRealTime = RGB_BLACK;
uint8_t xdata colorLightTime =0;
volatile uint8_t xdata RGBLightFlag=100;

typedef struct{
	uint8_t r;
	uint8_t g;
	uint8_t b;	
}RGB_t;

void rgbLedInit(void)
{
	
//	GPIO_Init(GPIO0, GPIO_PIN_0, GPIO_MODE_OUT_PP);
//	GPIO_Init(GPIO0, GPIO_PIN_1, GPIO_MODE_OUT_PP);
//	GPIO_Init(GPIO0, GPIO_PIN_2, GPIO_MODE_OUT_PP);	
//	
//	PWM_Init(PWM_PRESSEL_FOSC_D8, 255);
//	PWM_IndependentModeConfig(PWM0,0X00);
//	PWM_IndependentModeConfig(PWM1,0XFF);
//	PWM_IndependentModeConfig(PWM2,0XFF);
//	PWM_OutputStateConfig(0x00|PWM0|PWM1|PWM2,PWM_OUTPUTSTATE_ENABLE);
//	PWM_PolarityConfig(0x00|PWM0|PWM1|PWM2, PWM_POLARITY_NON_INVERT);
//	PWM_ITConfig(DISABLE,LOW);
//	PWM_Cmd(ENABLE);
}


unsigned char rgbledSetCode(unsigned char red_val, unsigned char green_val, unsigned char blue_val)
{
	if(red_val != 0)
		PWM_IndependentModeConfigEX(RGB_RED_PWM, PWM0_VALUE_MAX/256*red_val/5, PWM_OUTPUTSTATE_ENABLE);
	else
		PWM_IndependentModeConfigEX(RGB_RED_PWM, 0, PWM_OUTPUTSTATE_DISABLE);
	
	if(green_val != 0)
		PWM_IndependentModeConfigEX(RGB_GREEN_PWM, PWM0_VALUE_MAX/256*green_val/5, PWM_OUTPUTSTATE_ENABLE);
	else
		PWM_IndependentModeConfigEX(RGB_GREEN_PWM, 0, PWM_OUTPUTSTATE_DISABLE);

	if(blue_val != 0)
		PWM_IndependentModeConfigEX(RGB_BLUE_PWM, PWM0_VALUE_MAX/256*blue_val/5, PWM_OUTPUTSTATE_ENABLE);
	else
		PWM_IndependentModeConfigEX(RGB_BLUE_PWM, 0, PWM_OUTPUTSTATE_DISABLE);	
}

void RGB_light_set(uint8_t level)
{
	RGBLightFlag=level;
}

/*rgb color control*/
unsigned char rgbLedSetColor(rgb_color_e color)
{
	RGB_t RGB;
	float RGBLightRatio=RGBLightFlag*0.01;
	if(color > RGB_COLOR_MAX)
		return 0;
	if(colorRealTime == color )
	{
		if( colorLightTime == RGBLightFlag)
		{
			return 1;
		}
		else
		{
			colorLightTime=RGBLightFlag;
		}
	}
	else
	{
		colorRealTime = color;
	}
	switch(color)
	{
		case RGB_BLACK:
//			printf("rgb black\r\n");
			RGB.r=0;
			RGB.g=0;
			RGB.b=0;
			break;
		case RGB_GREEN:
	//		printf("rgb green\r\n");
			RGB.r=0;
			RGB.g=0xA0;
			RGB.b=0;
			break;
		case RGB_BLUE:
		//	printf("rgb blue\r\n");
			RGB.r=0;
			RGB.g=0;
			RGB.b=0xff;
			break;
		case RGB_RED:
		//	printf("rgb red\r\n");
			RGB.r=0x6F;
			RGB.g=0;
			RGB.b=0;
			break;
		case RGB_ORANGE:
		//	printf("rgb orange\r\n");
			RGB.r=0x40;
			RGB.g=50;
			RGB.b=0;
			break;
		case RGB_YELLOW:
		//	printf("rgb yellow\r\n");
			RGB.r=0x60;
			RGB.g=0xD0;
			RGB.b=0;
			break;
		case RGB_WHITE:
	   //     printf("rgb white\r\n");
			RGB.r=0x5f;
			RGB.g=0xE0;
			RGB.b=0xff;
			break;//RGB_WHITE
		default:
			break;
	}
	rgbledSetCode(RGB.r*RGBLightRatio, RGB.g*RGBLightRatio,RGB.b*RGBLightRatio);
	return 0;
}


void fan_power_switch(unsigned char en)
{
	if(en == 1)
	{
		GPIO_WriteHigh(GPIO2, GPIO_PIN_7);
	}
	else
	{
		GPIO_WriteLow(GPIO2, GPIO_PIN_7);
	}

}

unsigned char fan_set_value(unsigned char value)
{
	if(value > 100){
		printf("fan_set_value error,exceed max value\r\n");
		return 1;
	}
	if(value != 0)
		PWM_IndependentModeConfigEX(FAN_CONTROL_PWM, PWM0_VALUE_MAX/100*value, PWM_OUTPUTSTATE_ENABLE);
	else
		PWM_IndependentModeConfigEX(FAN_CONTROL_PWM, PWM0_VALUE_MAX/100*value, PWM_OUTPUTSTATE_DISABLE);
		
	return 0;
}

unsigned char fan_level_control(unsigned char level)
{
	if(level > FAN_LEVEL_MAX)
		return 1;
	
	if(fan_real_level != level)
	{
		printf("level set to %hu\r\n", fan_real_level);
		fan_real_level = level;
		//if(fan_real_level != 0)
			//PWM_IndependentModeConfigEX(FAN_CONTROL_PWM, PWM0_VALUE_MAX/100*fanLevelValue[fan_real_level], PWM_OUTPUTSTATE_ENABLE);
		//else
			//PWM_IndependentModeConfigEX(FAN_CONTROL_PWM, PWM0_VALUE_MAX/100*fanLevelValue[fan_real_level], PWM_OUTPUTSTATE_DISABLE);
		//if(fanDebugFlag == 0x0)
			fan_set_value(fanLevelValue[fan_real_level]);
	}
	return 0;
}

unsigned char xdata buzzer_flag = 0;
unsigned char xdata buzzer_state = 0;
uint32_t xdata buzzer_tick = 0;

void beep_on(void)
{
	buzzer_flag = 1;
}

void beep_control_handle(void)
{
	if(buzzer_flag == 1){
		buzzer_state = 1;
		buzzer_flag = 0;
		//BeepOnFlag = 1;
		PWM_IndependentModeConfigEX(PWM20,PWM2_VALUE_MAX/2, PWM_OUTPUTSTATE_ENABLE);
}
	
	if(buzzer_state == 1)
	{
		if(device_timeout_count(&buzzer_tick, 80) ==  TIME_OUT_WORK)
		{
			//BeepOnFlag = 0;
			buzzer_state = 0;
			PWM_IndependentModeConfigEX(PWM20,0, PWM_OUTPUTSTATE_ENABLE);
		}
		
	}
}

void disinfect_model_control(unsigned char state)
{
	if(disInfectWorkOnFlag == 0)
	{
		if(state != 0)
		{
			tm1668_Key_single_set(ePanelFlagDisinfect, 1);
			disInfectWorkOnFlag = 1;
			DISINFECT_PIN = 1;
		}	
	}
	else
	{
		if(state == 0)
		{
			tm1668_Key_single_set(ePanelFlagDisinfect, 0);
			DISINFECT_PIN = 0;
			disInfectWorkOnFlag = 0;
		}		
	}
}


bit disinfect_status_get(void)
{
	return disInfectWorkOnFlag;
}

void disinfect_status_invert(void)
{
	if(disInfectWorkOnFlag == 0)
	{
		tm1668_Key_single_set(ePanelFlagDisinfect, 1);
		disInfectWorkOnFlag = 1;
		DISINFECT_PIN = 1;
	}else{
		tm1668_Key_single_set(ePanelFlagDisinfect, 0);
		DISINFECT_PIN = 0;
		disInfectWorkOnFlag = 0;	
	}	
}


void ion_model_control(unsigned char state)
{
	if(IonWorkOnFlag == 0)
	{
		if(state != 0)
		{
			tm1668_Key_single_set(ePanelFlagIon, 1);
			IonWorkOnFlag = 1;
			ION_PIN = 1;
		}	
	}
	else
	{
		if(state == 0)
		{
			tm1668_Key_single_set(ePanelFlagIon, 0);
			ION_PIN = 0;
			IonWorkOnFlag = 0;
		}		
	}
}

bit ion_status_get(void)
{
	return IonWorkOnFlag;
}

void ion_status_invert(void)
{
	if(IonWorkOnFlag == 0)
	{
		tm1668_Key_single_set(ePanelFlagIon, 1);
		IonWorkOnFlag = 1;
		ION_PIN = 1;
	}else{
		tm1668_Key_single_set(ePanelFlagIon, 0);
		ION_PIN = 0;
		IonWorkOnFlag = 0;	
	}	
}




